﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data.SqlClient;

namespace DAL
{
   public class SysFlowDAL
    {
         /// <summary>
         /// 获取OA流程数据
         /// </summary>
         /// <param name="pageIndex"></param>
         /// <param name="pageSize"></param>
         /// <param name="total"></param>
         /// <returns></returns>
        public List<SysFLow> GetList(int pageIndex,int pageSize ,ref int total) {
            List<SysFLow> getData = null;
            using (CFA_OAEntities db = new CFA_OAEntities())
            {
                try
                {
                    getData = db.SysFLow.AsNoTracking().OrderByDescending(i => i.ID).Select(i => i).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); 
                    total = db.SysFLow.Count();
                }
                catch (Exception ex)
                {
                    //return Json(new { total = 0, rows = new List<object>() }, JsonRequestBehavior.AllowGet);
                }
            }
            return getData;
        }
        /// <summary>
        /// 导入OA
        /// </summary>
        /// <param name="flowID"></param>
        /// <returns></returns>
        public int ImprotOA(int flowID) {
            try
            {
                using (FlowDBEntities db=new FlowDBEntities())
                {
                    SqlParameter[] sqlParam = new SqlParameter[1];
                    sqlParam[0] = new SqlParameter("@FlowID1", flowID);
                    var value = db.Database.SqlQuery<int>("exec ImprotFlowToOAS @FlowID=@FlowID1", sqlParam).Cast<int>().First();
                    return value;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        //public int Save(SysFlows entity) {
        //    using (CFA_OAEntities db = new CFA_OAEntities())
        //    {
        //        int result = 0;
        //        if (entity.id != 0)
        //        {
        //            SysFLow model = db.SysFLow.Where(i => i.ID == entity.id).FirstOrDefault();
        //            if (model != null)
        //            {
        //                model.Code = entity.form_id;
        //                model.Name = entity.flow_name;
        //                model.Remark = entity.remark;
        //            }
        //        }
        //        else
        //        {
        //            SysFLow flow = new SysFLow();
        //            flow.Code = entity.form_id;
        //            flow.Name = entity.flow_name;
        //            flow.Remark = entity.remark;
        //            db.SysFLow.Add(flow);
        //        }
        //        result = db.SaveChanges();
        //        if (result > 0)
        //        {
        //            return Json(new { statu = 1 }, JsonRequestBehavior.AllowGet);
        //        }
        //    }
        //}
    }
}
